﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Xml.Linq;
using System.Configuration;
using System.Threading;
using System.Globalization;
using System.Text;
using System.Reflection;
using System.IO;
using System.Data;

using WebNet.Core;
using WebNet.Resources;
using WebNet.Configuration;

using NPOI.HSSF.UserModel;
using NPOI.HSSF.Extractor;
using DocumentFormat.OpenXml.Spreadsheet;
using DocumentFormat.OpenXml.Packaging;

using OfficeOpenXml;
using Excel;
using iTextSharp.text.pdf;
using iTextSharp.text.pdf.parser;

public partial class _Default : MultipleLangPage
{
    protected void Page_Load(object sender, EventArgs e)
    {
        //string plaintext = "RLWiDGYVGBuv4jICKtIGQw==";

        //string ciphertext = DM2CryptographyUnity.Decrypt(plaintext);

        //lblPlaintext.Text = plaintext;
        //lblCiphertext.Text = ciphertext;
        
        //foreach (string key in Request.Headers.Keys)
        //{

        //}

        //btnEnglish.Text = UIResource.English;
        //btnSimplifiedChinese.Text = UIResource.SimplifiedChinese;

        //var webNetConfigurationSection = ConfigurationManager.GetSection(WebNetConfigurationSection.ConfigurationSectionName) as WebNetConfigurationSection;
        //foreach (WebNetCountryConfigurationElement country in webNetConfigurationSection.Countries)
        //{
        //    Response.Write(country.CountryCode + ": " + country.EnableBotDetection);
        //    Response.Write("<br />");
        //}

        //string fileName = Server.MapPath("~/Doc/file_big.ppt");
        //if (File.Exists(fileName))
        //{
        //    //FileStream fs = new FileStream(fileName, FileMode.Open, FileAccess.Read);

        //    //byte[] fileData = new byte[fs.Length];

        //    //fs.Read(fileData, 0, (int)fs.Length);

        //    //string content = Encoding.UTF8.GetString(fileData);

        //    //string content = ReaderXlsxText(fileName);
        //    string content = ReaderPptText(fileName);

        //    Response.Write(content);
        //}        

        //string filePath = Server.MapPath("~/images/LoadAdvancedCaptcha.jpg");
        //string imageText = OCRHelper.GetTextFromImage(filePath);
        //Response.Write(imageText);
    }

    private string ReaderPptText(string fileName)
    {
        PptFileReader reader = new PptFileReader();

        string fileContent = reader.GetFileContent(fileName);

        return fileContent;
    }

    private string ReaderPptxText(string fileName)
    {
        PptxFileReader reader = new PptxFileReader();
        string fileContent = reader.GetFileContent(fileName);

        return fileContent;
    }

    private string ReaderPdfText(string fileName)
    {
        StringBuilder sb = new StringBuilder();

        PdfReader pdfReader = new PdfReader(fileName);
        int pageCount = pdfReader.NumberOfPages;

        for (int i = 1; i <= pageCount; i++)
        {
            string text = PdfTextExtractor.GetTextFromPage(pdfReader, i);

            sb.Append(text);
        }        

        return sb.ToString();
    }

    private string ReadExcelText(string fileName)
    {
        string fileContent = string.Empty;

        using (FileStream fs = new FileStream(fileName, FileMode.Open, FileAccess.Read))
        {
            HSSFWorkbook workbook = new HSSFWorkbook(fs);
            ExcelExtractor excelExtractor = new ExcelExtractor(workbook);

            //remove sheet tag
            fileContent = System.Text.RegularExpressions.Regex.Replace(excelExtractor.Text, "Sheet\\d+\\n", string.Empty);            
        }        

        return fileContent;
    }
    
    private string ReaderXlsxText(string fileName)
    {
        StringBuilder sb = new StringBuilder();

        //epplus
        using (var package = new ExcelPackage(new FileInfo(fileName)))
        {
            ExcelWorkbook workbook = package.Workbook;
            if (workbook != null)
            {
                foreach (var worksheet in workbook.Worksheets)
                {
                    //int rowCount = worksheet.Dimension.End.Row;
                    foreach (var cell in worksheet.Cells)
                    {
                        string cellText = cell.Text;
                        sb.Append(" " + cellText + " ");
                    }   
                }
            }
        }

        //excel data reader
        //using (FileStream fs = new FileStream(fileName, FileMode.Open))
        //{
        //    IExcelDataReader dataReader = ExcelReaderFactory.CreateOpenXmlReader(fs);
        //    DataSet ds = dataReader.AsDataSet();

        //    while (dataReader.Read())
        //    {

        //    }

        //    dataReader.Close();
        //}       

        //open xml sdk
        //using (SpreadsheetDocument doc = SpreadsheetDocument.Open(fileName, false))
        //{
        //    var workSheetParts = doc.WorkbookPart.Workbook.WorkbookPart.WorksheetParts;
        //    SharedStringTable stringTable = doc.WorkbookPart.SharedStringTablePart.SharedStringTable;

        //    foreach (var workSheetPart in workSheetParts)
        //    {
        //        foreach (var row in workSheetPart.Worksheet.Descendants<Row>())
        //        {
        //            foreach (Cell cell in row.Descendants<Cell>())
        //            {
        //                string cellText = GetCellText(cell, stringTable);
        //                sb.Append(" " + cellText + " ");
        //            }
        //        }
        //    }
        //}

        return sb.ToString();
    }

    private string GetCellText(Cell cell, SharedStringTable stringTable)
    {
        if (cell.ChildElements.Count == 0)
        {
            return string.Empty;
        }
            
        string val = cell.CellValue.InnerText;

        //handle shared string
        if (cell.DataType != null && cell.DataType == CellValues.SharedString)
        {
            val = stringTable.ChildElements[int.Parse(val)].InnerText;
        }
            
        return val;
    }


    private string ReadWordText(string fileName)
    {
        string fileContent = string.Empty;

        return fileContent;     
    }

    private string ReadPptText(string fileName)
    {
        string fileContent = string.Empty;

        return fileContent;
    }

    protected void btnEnglish_Click(object sender, EventArgs args)
    {
        
    }

    protected void btnSimplifiedChinese_Click(object sender, EventArgs args)
    {
        
    }
    protected void buttonReadValueBySettingKey_Click(object sender, EventArgs e)
    {
        if (string.IsNullOrWhiteSpace(textBoxSettingKey.Text))
        {
            return;
        }

        lblSettingValue.Text = ConfigurationManager.AppSettings[textBoxSettingKey.Text.Trim()];
    }
}
